package com.hethongviet.vending.pages;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.hethongviet.vending.AppMain;
import com.hethongviet.vending.Constant;
import com.hethongviet.vending.AppConfig;
import com.hethongviet.vending.R;
import com.hethongviet.vending.Utils;
import com.hethongviet.vending.helper.GeneralActivity;
import com.hethongviet.vending.helper.db.BoxInventorySQLite;
import com.hethongviet.vending.helper.db.BoxSQLite;
import com.hethongviet.vending.helper.db.ProductSellSQLite;
import com.hethongviet.vending.helper.db.ProductServiceSQLite;
import com.hethongviet.vending.model.Transaction;
import com.hethongviet.vending.network.Network;
import com.hethongviet.vending.network.CONSTANTS.SERVICE;

public class PauseActivity extends GeneralActivity
{
	public static final String EXTRA_REQUEST_TYPE = "extra_request_type";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.page_pause);

	}

	@Override
	protected void onStart()
	{
		super.onStart();

		int requestType = getIntent().getIntExtra(
				PauseActivity.EXTRA_REQUEST_TYPE, Constant.REQUEST_NONE);

		switch (requestType)
		{
			case Constant.REQUEST_ERROR_DEVICE:

				break;
			case Constant.REQUEST_FAIL_LOAD_INFO:

				break;
			case Constant.REQUEST_UPDATE_DATA:
				/* Update data transaction */
				updateData();

				Utils.debug("Update database online successful!!!!!!");
				/* Back to serving */
				Intent intent = new Intent(PauseActivity.this,
						StandbyActivity.class);
				startActivity(intent);
				break;

			default:
				break;
		}
	}

	private void updateData()
	{
		/* Initiate database */
		ProductSellSQLite productSellSQLite = new ProductSellSQLite(this);
		ProductServiceSQLite productServiceSQLite = new ProductServiceSQLite(
				this);
		BoxInventorySQLite boxInventorySQLite = new BoxInventorySQLite(this);
		BoxSQLite boxSQLite = new BoxSQLite(this);

		/* Get products sell out */

		if (AppConfig.USE_SEND_ALL_DATA)
		{
			if (AppConfig.IS_NETWORK_AVAILABLE)
				Network.sendDataToServer(productSellSQLite.getAll(),
					productServiceSQLite.getAll(), boxInventorySQLite.getAll(),
					boxSQLite.getAll());
		}
		else
		{
			if (AppConfig.IS_NETWORK_AVAILABLE)
				Network.sendBoxs(AppMain.getInstance().getListBoxItems());
		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event)
	{
		if (KeyEvent.KEYCODE_MENU == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_LEFT == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_DOWN == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_RIGHT == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_UP == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_CENTER == event.getKeyCode()
				|| KeyEvent.KEYCODE_CALL == event.getKeyCode()
				|| KeyEvent.KEYCODE_CAMERA == event.getKeyCode()
				|| KeyEvent.KEYCODE_ENDCALL == event.getKeyCode()
				|| KeyEvent.KEYCODE_HOME == event.getKeyCode()
				|| KeyEvent.KEYCODE_SEARCH == event.getKeyCode()
				|| KeyEvent.KEYCODE_BACK == event.getKeyCode())
		{
			return Utils.disableAllKey(event);
		}
		else
			return super.dispatchKeyEvent(event);

	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		return createDialogStatusServer(id);
	}

	@Override
	public void onAttachedToWindow()
	{
		if (AppConfig.USE_ATTACH_WINDOW_FOR_DISABLE_HOME_KEY)
		{
			this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
			super.onAttachedToWindow();
		}
	}

	@Override
	public void onNetworkMessageIncomming(Transaction transaction)
	{
		if (transaction == null)
		{
			Utils.debug("Null -----------received");
			return;
		}

		String message = transaction.getMessageReceived();
		Utils.debug("Receive message = " + message);

		if (message.equalsIgnoreCase(SERVICE.RESULT_OK))
		{
			Utils.debug("OK!!!!!!!!!!!");
			hideProgressDialog();

			return;
		}

	}

	@Override
	public void onConnectFail(int typeError)
	{
		Utils.debug("Connect fail!.......................");
		hideProgressDialog();

		showDialogWithStatus(typeError);
	}

}
